<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2017/6/12
 * Time: 10:15
 */

namespace Admin\Controller;


use Admin\Logic\Controller\MainController;
use Admin\Model\QrCodeModel;
use WeiChat\api\QrCode;

class QrCodeController extends MainController{
    /**
     * @var QrCodeModel
     */
    protected $qrCodeModel = null;
    public function _initialize(){
        parent::_initialize();
        $this->qrCodeModel = new QrCodeModel();
    }
    public function qrCode(){
        $qrObj = new QrCode();
        $url = I('get.url','','string');
        $qrObj->show($url);
    }
    public function qrList(){
        $this->assign ( 'qrList',  $this->qrCodeModel->QrCodeList() );
        $this->display();
    }
    public function qrAdd(){
        if(!IS_POST){
            $this->assign('hospitalList',$this->hospitalModel->HospitalList());
            $this->display('qrEdit');
        }else{
            $data['name'] = I("post.name",'','string');
            $data['url'] = I("post.url",'','string');
            $data['h_id'] = I("post.h_id",'','int');
            $data['remark'] = I("post.remark",'','string');
            $res = $this->qrCodeModel->QrCodeAdd($data);
            if($res){
                $this->success('添加成功',U("QrCode/qrList"));
            }else{
                $this->error('删除失败');
            }
        }
    }
    public function qrEdit(){
        if(!IS_POST){
            $this->assign('hospitalList',$this->hospitalModel->HospitalList());
            $id = I('get.id',null,'int');
            $id || die;
            $this->assign ( 'info',  $this->qrCodeModel->QrCodeFind($id) );
            $this->display();
        }else{
//            var_dump($_REQUEST);die;
            $res = $this->qrCodeModel->QrCodeEdit();
            if($res !== false){
                $this->success("保存成功");
            }else{
                $this->error('保存失败');
            }
        }
    }
    public function qrDelete(){
//        var_dump(I('get.'));die;
        $id = I('get.id',null,'int');
        if($id){
            $res = $this->qrCodeModel->QrCodeDel($id);
        }
        if($res){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }
}